package com.oocl.msgProcess;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;

import com.oocl.friendListDB.FriendList;
import com.oocl.tm.protocol.MsgPackage;

public class ChatProcess {
	


	public ChatProcess(){
	
	}
	
	public static void chatProcess(MsgPackage msg, JTextArea chatTa,String name){
		if(!name.equals(msg.getSender())){
			Calendar time = Calendar.getInstance();
			String sender = msg.getSender();
			String receiver = msg.getReceiver();
			String content = msg.getContent();
			chatTa.append(time.get(Calendar.HOUR_OF_DAY)+":"+time.get(Calendar.MINUTE)+ "  "+ sender + " to " + receiver +  " :"+"\n" );
			chatTa.append("\t     "+content+"\n");
		}
	}
	
	public static void notiryProcess(MsgPackage msg, ButtonGroup btnGroup,String name) throws InterruptedException{
			HashMap<String, Boolean> map = msg.getOnlineMap();
			Enumeration<AbstractButton> btns = btnGroup.getElements();
			List<AbstractButton> buttonList = new ArrayList<AbstractButton>();
			while(btns.hasMoreElements()){
				AbstractButton btn = btns.nextElement();
				buttonList.add(btn);
			}
			for(String x:map.keySet()){
				if(name.equals(x)){
					continue;
				}else{
					boolean isOnline = map.get(x);
					for (int i = 0; i < buttonList.size(); i++) {
						if(buttonList.get(i).getText().equals(x)){
							Timer timer1 = new Timer();
							Timer timer2 = new Timer();
							if(isOnline){
								timer1.schedule(new FlashOn(buttonList.get(i)), 1);
							}else{
								timer2.schedule(new FlashOff(buttonList.get(i)), 1);
							}
							
						}
					}
					
				}
			}
	}
	
	
	
	
}

class FlashOn extends TimerTask{
	
	private AbstractButton btn;
	
	public FlashOn(AbstractButton btn){
		this.btn = btn;
	}

	public void run() {
		ImageIcon on = new ImageIcon("on.png");
		ImageIcon off = new ImageIcon("off.png");
		for (int i = 0; i < 6; i++) {
			try {
				btn.setIcon(on);
				Thread.sleep(100);
				btn.setIcon(off);
				Thread.sleep(100);
				btn.setIcon(on);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}

class FlashOff extends TimerTask{
	
	private AbstractButton btn;
	
	public FlashOff(AbstractButton btn){
		this.btn = btn;
	}

	public void run() {
		ImageIcon on = new ImageIcon("on.png");
		ImageIcon off = new ImageIcon("off.png");
		for (int i = 0; i < 6; i++) {
			try {
				btn.setIcon(on);
				Thread.sleep(10);
				btn.setIcon(off);
				Thread.sleep(10);
				btn.setIcon(off);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}